package cloud.dandanit.itservice.utils;

import cloud.dandanit.itservice.service.GiveLikeService;
import cloud.dandanit.itservice.service.RateService;
import cloud.dandanit.itservice.service.StatisticsAnalysisService;
import cloud.dandanit.itservice.service.ViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author luokai
 */
@SuppressWarnings("AlibabaRemoveCommentedCode")
@Component
public class ScheduledTask {

    @Autowired
    private GiveLikeService giveLikeService;
    @Autowired
    private RateService rateService;
    @Autowired
    private ViewService viewService;

    @Autowired
    private StatisticsAnalysisService statisticsAnalysisService;

    /**
     * Redis向mysql保存评分
     * 每20s执行一次
     */
    @Scheduled(cron = "0/20 * * * * ?")
    public void redisPersistenceForRate() {

        Date date = new Date();
        System.out.println(date+"执行了定时器保存评分");
        rateService.submitRateFromRedisToMysql();
    }

    /**
     * Redis向mysql保存点赞数
     * 每20s执行一次
     */
    @Scheduled(cron = "0/20 * * * * ?")
    public void redisPersistenceForLikeNum() {

        Date date = new Date();
        System.out.println(date+"执行了定时器保存点赞数");
        giveLikeService.addGiveLikeNumFromRedisToMysql();
    }

    /**
     * Redis向mysql保存浏览数
     * 每分钟执行一次
     */
    @Scheduled(cron = "0/20 * * * * ?")
    public void redisPersistenceForViewNum() {

        Date date = new Date();
        System.out.println(date+"执行了定时器保存点赞数");
        viewService.addViewNumFromRedisToMysql();
    }

    /**
     * 每小时执行一次
     * （SpringBoot只允许只写前6位）
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void createStatisticsByDay() {
        Date date = new Date();
        System.out.println(date+"执行了定时器统计数据");
        //获取上一天的日期
        String day = DateUtil.formatDate(DateUtil.addDays(date, 0));
        statisticsAnalysisService.createStatisticsByDay(day);

    }
}
